package com.zhentao.controller;

import com.zhentao.dtos.Result;
import com.zhentao.model.entity.h5.CartInfo;
import com.zhentao.service.CartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("cartInfo")
public class CartInfoController {
    @Autowired
    private CartInfoService cartInfoService;

    @RequestMapping("findAllById")
    public Result findAllById( Long id ) {
        try {
            List< CartInfo > list = cartInfoService.findAllById( id );
            return Result.success( list );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @RequestMapping("add")
    public Result add( Long userId, Long skuId, Integer skuNum, BigDecimal cartPrice ) {
        try {
            CartInfo cartInfo = new CartInfo();
            cartInfo.setUserId( userId );
            cartInfo.setSkuId( skuId );
            cartInfo.setCartPrice( cartPrice );
            cartInfo.setSkuNum( skuNum );
            cartInfo.setIsChecked( 0 );
            cartInfo.setCreateTime( new Date() );
            cartInfoService.add( cartInfo );
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }
}
